package com.mq.dpca.ymtc;

import java.util.UUID;

import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.QueueConnection;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

import com.alibaba.fastjson.JSON;
import com.ibm.mq.MQException;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.msg.client.wmq.WMQConstants;
import com.mq.ghac.util.EsbHeader;

public class TEST_SEND {

	public static void main(String[] args) throws MQException {
		
		/*EsbHeader esbHeader=new EsbHeader();
		esbHeader.setRequestID(UUID.randomUUID().toString().replace("-", ""));
		esbHeader.setServiceName("Q1");
		esbHeader.setToken("cf26b081341ff58458436bf0ef540c30");
		esbHeader.setOutQ("");
		String esbHeader_json = JSON.toJSONString(esbHeader);
		*/
		Baseparams baseparams=new Baseparams();
			baseparams.setReqSerialNo(UUID.randomUUID().toString().replace("-", ""));
			baseparams.setClientCode("NAST_SOURCE");
			baseparams.setTradeCode("mq_nast_001");
		String baseparams_json = JSON.toJSONString(baseparams);
		
		sendMsg("测试消息",baseparams_json);
	}

	public static void sendMsg(String msg,String baseparams_json) {
		MQQueueConnectionFactory connectionFactory = null;
		QueueConnection queueConn = null;
		QueueSession queueSession = null;
		QueueSender queueSender = null;
		TextMessage message = null;

		try {
			connectionFactory = new MQQueueConnectionFactory();
			connectionFactory
					.setConnectionNameList("192.168.133.10(1414)");
			connectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
			connectionFactory.setQueueManager("NAST_QMGR_001");
			connectionFactory.setChannel("SYSTEM.ADMIN.SVRCONN");
			queueConn = connectionFactory.createQueueConnection(null, null);
			queueSession = queueConn.createQueueSession(false,
					Session.AUTO_ACKNOWLEDGE);
			queueSender = queueSession.createSender(queueSession
					.createQueue("Q1"));
			queueSender.setDeliveryMode(DeliveryMode.PERSISTENT);
			message = queueSession.createTextMessage(msg);
			message.setStringProperty("Esbheader", baseparams_json);
			queueSender.send(message);
			System.out.println("消息已发送成功");
		}

		catch (Exception e) {
			e.printStackTrace();
		} finally {

			if (queueSender != null) {
				try {
					queueSender.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
			if (queueSession != null) {
				try {
					queueSender.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}

	}

}
